package com.th.supcom.pds.impl.service.internal.phamapply.command;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.InpPhamApplyDetailSummary;
import com.th.supcom.pds.domain.vo.InpPhamApplyDetailSummaryVO;
import com.th.supcom.pds.domain.vo.InpPhamDispenseDetailVO;
import com.th.supcom.pds.impl.common.enums.BMSApplyClassEnum;
import com.th.supcom.pds.impl.common.enums.BMSItemClassEnum;
import com.th.supcom.pds.impl.common.enums.BMSRcptOnClassCodeEnum;
import com.th.supcom.pds.impl.common.enums.PhamMaterialTypeIdEnum;
import com.th.supcom.pds.impl.service.internal.phamapply.PhamApplyConvertUtils;

/**
 * 获取药品基本信息、供药属性以及库存信息
 * 
 * @author leaqua@qq.com
 */
public class ObtainPhamInfo extends AbstractPhamApplyManagerBean implements Command
{

    @Override
    public boolean execute (Context arg0) throws Exception
    {
        PhamDispenseContext context = (PhamDispenseContext) arg0;
        List <String> phamStdCodeList = context.getPhamStdCodeList ();
        Map <String, List <InpPhamDispenseDetailVO>> dispenseMap = context.getDispenseDetailByPhamStdCode ();
        InpPhamApplyDetailSummaryVO inpPhamApplyDetailSummaryVO = new InpPhamApplyDetailSummaryVO ();
        inpPhamApplyDetailSummaryVO.setPhamStdCodeList (phamStdCodeList);
        inpPhamApplyDetailSummaryVO.setDispensary (context.getDispensary ());
        List <InpPhamApplyDetailSummary> phamInfoList = this.getInpPhamApplyInfoQueryService ().findInpApplyBasicPhamInfo (inpPhamApplyDetailSummaryVO);
        Map <String, InpPhamApplyDetailSummary> phamMap;
        phamMap = PhamApplyConvertUtils.convertPhamInfoListToMapByPhamStdCode (phamInfoList);
        Set <Map.Entry <String, InpPhamApplyDetailSummary>> set = phamMap.entrySet ();
        for (Iterator <Map.Entry <String, InpPhamApplyDetailSummary>> it = set.iterator (); it.hasNext ();)
        {
            Map.Entry <String, InpPhamApplyDetailSummary> entry = (Map.Entry <String, InpPhamApplyDetailSummary>) it.next ();
            InpPhamApplyDetailSummary phamInfo = entry.getValue ();
            List <InpPhamDispenseDetailVO> temp = dispenseMap.get (entry.getKey ());
            double totalDispenseAmount = 0;
            for (InpPhamDispenseDetailVO inpPhamDispenseDetailVO : temp)
            {
                totalDispenseAmount += inpPhamDispenseDetailVO.getApplyAmount ();
                inpPhamDispenseDetailVO.setPhamName (phamInfo.getPhamName ());
                inpPhamDispenseDetailVO.setDispensePrice (inpPhamDispenseDetailVO.getFactor () * phamInfo.getPhamPrice ());
                inpPhamDispenseDetailVO.setLeastAbleUnit (phamInfo.getPhamUnit ());
                if (phamInfo.getPhamMaterialType ().equals (PhamMaterialTypeIdEnum.ChineseTraditionMed.getEnumCode ()))
                {
                    inpPhamDispenseDetailVO.setClassOnRcptCode (BMSRcptOnClassCodeEnum.ChinesePatentDrugCharge.getEnumCode ());
                    inpPhamDispenseDetailVO.setItemClass (BMSItemClassEnum.ChinesePatentDrug.getEnumCode ());
                }
                else
                {
                    inpPhamDispenseDetailVO.setClassOnRcptCode (BMSRcptOnClassCodeEnum.WesterMedicineCharge.getEnumCode ());
                    inpPhamDispenseDetailVO.setItemClass (BMSItemClassEnum.WesterMedicine.getEnumCode ());
                }
                inpPhamDispenseDetailVO.setApplyClass (BMSApplyClassEnum.InpPhamDispense.getEnumCode ());
                inpPhamDispenseDetailVO.setAllocation (phamInfo.getAllocation ());
            }
            phamInfo.setDetailSumAmount (totalDispenseAmount);
            phamInfo.setNurseUnit (temp.get (0).getNurseUnit ());
        }
        context.setPhamInfoList (phamInfoList);
        context.setPhamInfoMap (phamMap);
        return false;
    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (ObtainPhamInfo.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
